#include <iostream>
#include <list>

using namespace std;
//案例描述：将Person自定义数据类型进行排序，Person中属性有姓名、年龄、身高
//排序规则：按照年龄进行升序，如果年龄相同按照身高进行降序

class Person {
public:
    string name;//姓名
    int age;//年龄
    int height;//身高
    Person(string name, int age, int height) {
        this->name = name;
        this->age = age;
        this->height = height;
    }
};

bool ComparePerson(Person &p1, Person &p2) {
    if (p1.age == p2.age) {//年龄相同 按照身高降序
        return p1.height > p2.height;
    } else {//按照年龄升序
        return p1.age < p2.age;
    }
}

void test() {
    list<Person> l;//创建容器

    Person p1("刘备", 35, 175);//准备数据
    Person p2("曹操", 45, 180);
    Person p3("孙权", 40, 170);
    Person p4("赵云", 25, 190);
    Person p5("张飞", 35, 160);
    Person p6("关羽", 35, 200);

    l.push_back(p1);
    l.push_back(p2);
    l.push_back(p3);
    l.push_back(p4);
    l.push_back(p5);
    l.push_back(p6);

    for (list<Person>::iterator it = l.begin(); it != l.end(); ++it) {
        cout << "name:" << it->name << ",age=" << it->age << ",height=" << it->height << endl;
    }

    cout << "=========" << endl;
    l.sort(ComparePerson);//排序

    for (list<Person>::iterator it = l.begin(); it != l.end(); ++it) {
        cout << "name:" << it->name << ",age=" << it->age << ",height=" << it->height << endl;
    }
}

int main() {
    test();
    return 0;
}